﻿//using NuGet.Protocol.Core.Types;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using static System.Net.WebRequestMethods;
using Microsoft.EntityFrameworkCore;

using System.Threading.Tasks;


namespace PanhuaWin
{
    public partial class FormLogin :UILoginForm
    {

        public FormLogin()
        {
            InitializeComponent();
            this.ButtonLoginClick += FormLogin_ButtonLoginClick;
            this.ButtonCancelClick += FormLogin_ButtonCancelClick;
            this.UserName = "";
            this.Password = "";


        }

        private void FormLogin_ButtonCancelClick(object sender, EventArgs e)
        {
           Application.Exit();
        }

        private async void FormLogin_ButtonLoginClick(object sender, EventArgs e)
        {
            checkIn();
            
         
          

        }

        private void checkIn()
        {
            //  MessageBox.Show("123");
            string username = this.UserName;
            string password = this.Password;
            string 验证码 = this.uiTextBox验证码.Text;
            string uuid = this.uiLabeluuid.Text;
            //if (验证码 == "")
            //{
            //    MessageBox.Show("验证码不能为空", "登录失败", MessageBoxButtons.OK);

            //    return;
            //}
            if (password == "" || username == "")
            {
                MessageBox.Show("用户名密码不能为空", "登录失败", MessageBoxButtons.OK);
                return;
            }



         
            string posturl;
            posturl = pub.Sys_User_Login_Url;

            LoginUser loginUser = new LoginUser();
            loginUser.uuid = uuid;
            loginUser.userName = this.UserName;
            loginUser.Password = this.Password;
            loginUser.verificationCode = "1";
            string loginUserJson = Json.Serialize(loginUser);
            string s = pub.HttpPost(posturl, loginUserJson);
            LoginUserResult loginUserResult = new LoginUserResult();
            loginUserResult = Json.Deserialize<LoginUserResult>(s);
            if (loginUserResult != null)
            {
                if (loginUserResult.status.ToLower() == "true")
                {
                    pub.sys_user = pub.GetUserByUserName(loginUser.userName);
                    if (pub.sys_user != null)
                    {
                        FormMain fm = new FormMain();
                        pub.sys_user.Token = loginUserResult.data.token;
                        fm.Show();
                        this.Hide();
                    }
                    else
                    {
                        pub.sys_user.Token = null;
                        MessageBox.Show(loginUserResult.message, "登录失败", MessageBoxButtons.OK);
                        this.uiTextBox验证码.Text = "";
                        Reflesh();
                        return;
                    }
                }
                else
                {
                    MessageBox.Show(loginUserResult.message, "登录失败", MessageBoxButtons.OK);
                    this.uiTextBox验证码.Text = "";
                    Reflesh();
                    return;
                }
            }
            else
            {
                MessageBox.Show("服务器有异常请重试", "登录失败", MessageBoxButtons.OK);
                this.uiTextBox验证码.Text = "";
                Reflesh();
                return;
            }
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            Reflesh();
           
        }

        private void Reflesh()
        {
            string pic;
            pic = pub.HttpGet(pub.Sys_User_getVierificationCode);

            VierificationCodeImg vierificationCodeImg = new VierificationCodeImg();
            vierificationCodeImg = Json.Deserialize<VierificationCodeImg>(pic);
            byte[] imageBytes = Convert.FromBase64String(vierificationCodeImg.img);
            //读入MemoryStream对象
            MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
            memoryStream.Write(imageBytes, 0, imageBytes.Length);
            this.uiLabeluuid.Text = vierificationCodeImg.uuid;

            //二进制转成图片保存
            System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);



            this.pictureBox验证码.Image = image;

        }

        private void pictureBox验证码_Click(object sender, EventArgs e)
        {
            Reflesh();
        }

        private void uiTextBox验证码_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==13)
            {
                checkIn();
            }
        }
    }
}
